package com.art.sunflower.controller;

import com.art.sunflower.exception.NullClassroomException;
import com.art.sunflower.service.ClassroomService;
import com.art.sunflower.util.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping(value = "/classroom")
@Api(value = "教室控制器")
public class ClassroomController {

    @Resource
    ClassroomService classroomService;

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "分页获取班级数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", dataType = "int", paramType = "query", defaultValue = "5"),
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", paramType = "query", defaultValue = "1"),})
    @GetMapping()
    public Message getList(Integer pageSize, Integer pageNo) {
        try {
            return Message.createSuc(classroomService.getList(pageSize, pageNo));
        } catch (NullClassroomException e) {
            e.printStackTrace();
            return Message.createErr("行政班不存在");
        }
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "增加一条班级数据")
    @PostMapping()
    public Message add(Integer teacherId, String name, String grade, String message, Integer num) {
        classroomService.add(teacherId, name, grade, message, num);
        return Message.createSuc("添加成功");
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "修改一条班级数据")
    @PutMapping()
    public Message update(Integer id, Integer teacherId, String name, String grade, String claMsg, Integer clsNum) {
        String msg = "修改成功";
        try {
            classroomService.update(id, teacherId, name, grade, claMsg, clsNum);
            return Message.createSuc(msg);
        } catch (NullClassroomException e) {
            e.printStackTrace();
            msg = "班级不存在";
        }
        return Message.createErr(msg);
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "删除一条班级数据")
    @DeleteMapping(value = "/{id}")
    public Message delete(@PathVariable Integer id) {
        String msg = "删除成功";
        try {
            classroomService.delete(id);
            return Message.createSuc(msg);
        } catch (NullClassroomException e) {
            e.printStackTrace();
            msg = "班级不存在";
        }
        return Message.createErr(msg);
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "查看一条班级数据")
    @GetMapping(value = "/{id}")
    public Message get(@PathVariable Integer id) {
        String msg;
        try {
            return Message.createSuc(classroomService.getById(id));
        } catch (NullClassroomException e) {
            e.printStackTrace();
            msg = "班级不存在";
        }
        return Message.createErr(msg);
    }

    @RequiresRoles(value = {"teacher"})
    @GetMapping("classroomListByTeacher")
    @ApiOperation("分页获取幼儿班列表")
    public Message findClassroomByTeacher(@RequestParam("teacherName") String teacherName, @RequestParam Integer pagenum, @RequestParam Integer pagesize) {

        return Message.createSuc(classroomService.findClassroomByTeacher(teacherName, pagenum, pagesize));
    }

}
